//
//  ByNoticeHistoryListVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/16.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByNoticeHistoryListVC.h"
#import "ByNoticeCirclesView.h"
#import "ByLotteryNoticeDetailVC.h"

@interface ByNoticeHistoryListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ByNoticeHistoryListVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"历史开奖";
    
    [self initViews];
    
    [self loadData];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}


- (void)loadData{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中"];
    if (self.netModel.category_id.integerValue == 1001) {
        [ByNumberApi getColorBallPrizeHistory:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            SLog(@"双色球开奖历史 = %@", result.data);
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in result.data[@"data"][@"his"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                
                NSMutableArray *dataArray =  [ByLotteryAlgorithmHandle getModelsArrayWithNumString:netModel.result_num];
                netModel.dataArray = [dataArray copy];
                
                
                [self.dataArray addObject:netModel];
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
             [self.tableView.mj_header endRefreshing];
        }];
        
    }else if (self.netModel.category_id.integerValue == 2001){
        
        [ByNumberApi getLottoPrizeHistory:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in result.data[@"data"][@"his"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
                NSMutableArray *dataArray =  [ByLotteryAlgorithmHandle getModelsArrayWithNumString:netModel.result_num];
                netModel.dataArray = [dataArray copy];
                
                [self.dataArray addObject:netModel];
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
             SLog(@"大乐透开奖历史 = %@", result.data);
        } failure:^(ByNetBaseResult *result) {
            [self.tableView.mj_header endRefreshing];
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else{
        
        [self.tableView.mj_header endRefreshing];
    }
    
    
}

- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
        
        
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.right.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(0);
        make.top.equalTo(cell.contentView).offset(10);
    }];
    backgroundView.layer.cornerRadius = 9;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *arrowView = [[UIImageView alloc] init];
    [backgroundView addSubview:arrowView];
    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-10);
        make.centerY.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    arrowView.image = [UIImage imageNamed:@"youjiantou1111"];
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = [NSString stringWithFormat:@"第%@期", netModel.number];
    CGFloat width = [numLabel.text getRectOfStringWithSize:CGSizeMake(1000, 20) font:13].size.width + 10;
    [backgroundView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(11);
        make.top.equalTo(backgroundView).offset(15);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(@(width));
    }];
    
    numLabel.font = FONT(14);
    numLabel.textColor = [UIColor blackColor];
    numLabel.textAlignment = NSTextAlignmentLeft;

    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [backgroundView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numLabel.mas_right).offset(10);
        make.right.equalTo(arrowView.mas_left).offset(-10);
        make.height.mas_equalTo(@15);
        make.top.equalTo(numLabel.mas_top).offset(0);
    }];
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = FONT(12);
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.text = netModel.open_time;
    
    
    ByNoticeCirclesView *circlesView = [[ByNoticeCirclesView alloc] init];
    [backgroundView addSubview:circlesView];
    [circlesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.top.equalTo(numLabel.mas_bottom).offset(5);
        make.right.equalTo(backgroundView).offset(-40);
        make.height.mas_equalTo(@55);
    }];
    circlesView.itemsAry = netModel.dataArray;
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    ByLotteryNoticeDetailVC *vc = [[ByLotteryNoticeDetailVC alloc] init];
    vc.ID = netModel.ID;
    vc.isFromHostoryList = YES;
    [self by_normalPush:vc hasNavi:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
